package com.happyass.service.impl;

import com.happyass.common.entity.ResultEntity;
import com.happyass.dao.UserDao;
import com.happyass.pojo.Evaluate;
import com.happyass.pojo.User;
import com.happyass.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public List<User> selectAll() {
        return userDao.findAll();
    }
//BeanUtil可替代  返回ResultEntity<User>
    //新增
    @Override
    public HashMap<String, Object> insertUser(User user,String userArr) throws ParseException {
        HashMap<String, Object> map = new HashMap<>();
        User userInfo = userDao.findByUserName(user.getUserName());
        if (userInfo!=null){
            map.put("info","新增会员失败，重复，请重试");
            return map;
        }
        else {
            user.setUserStatus(1);
            user.setUserAddr(userArr);
            userDao.save(user);
            map.put("info","新增会员成功");
            return map;
        }
    }

    @Override
    public User selectById(Integer userId) {
      return userDao.findById(userId).get();
    }

    //修改
    @Override
    public boolean edit(User user) {
        try{
            //查询要修改的用户对象
            User userInfo = userDao.findById(user.getUserId()).get();
            String userUsername = user.getUserName();
            String userPasswd = user.getUserPasswd();
            String userTel = user.getUserTel();
            String userIdenty = user.getUserIdenty();
            String userBirth = user.getUserBirth();
            String userLocalName = user.getUserLocalName();
            String userAddr = user.getUserAddr();
            userInfo.setUserName(userUsername);
            userInfo.setUserPasswd(userPasswd);
            userInfo.setUserTel(userTel);
            userInfo.setUserLocalName(userLocalName);
            userInfo.setUserBirth(userBirth);
            userInfo.setUserIdenty(userIdenty);
            userInfo.setUserAddr(userAddr);
            //修改保存
            userDao.save(userInfo);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    //删除
    @Override
    public boolean del(Integer userId) {
        try {
            userDao.deleteById(userId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    //修改状态
    @Override
    public Boolean editStatus(Integer userId, Integer userStatus) {
        User user = userDao.findById(userId).get();
        if (user.getUserStatus()!=userStatus) {
            user.setUserStatus(userStatus);
            userDao.save(user);
            return true;
        } else {
            return false;
        }

    }
    //批量删除
    @Override
    public boolean delAll(String userIds) {
        try {
            String[] userId = userIds.split(",");
            userDao.deleteAllUser(Arrays.asList(userId));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    //分页查询
    @Override
    public Page<User> selectAllUser(Integer page, Integer row) {
        Pageable pageable = PageRequest.of(page - 1, row);
        return userDao.findAll(pageable);
    }
}


//    @Override
//    @Transactional
//    public ResultEntity<User> insertUser(User user) {
//        final User temp = userDao.findByUserUsername(user.getUserUsername());
//        if (temp != null) {
//            try {
//                User userinfo = new User();
//                String userUsername = user.getUserUsername();
//                String userPasswd = user.getUserPasswd();
//                String userTel = user.getUserTel();
//                String userIdenty = user.getUserIdenty();
//                Date userBirth = user.getUserBirth();
//                String userName = user.getUserName();
//                String userAddr = user.getUserAddr();
//                userinfo.setUserUsername(userUsername);
//                userinfo.setUserPasswd(userPasswd);
//                userinfo.setUserTel(userTel);
//                userinfo.setUserName(userName);
//                userinfo.setUserBirth(userBirth);
//                userinfo.setUserIdenty(userIdenty);
//                userinfo.setUserAddr(userAddr);
//                userDao.save(userinfo);
//                return new ResultEntity<>(ResultEntity.ResultStatus.SUCCESS.status,
//                        "新增成功", user);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }return new ResultEntity<>(ResultEntity.ResultStatus.FAILED.status,
//                "用户名重复");
//    }
//}
